<?php

namespace App\Models;


class MemberWaterBill extends BaseModel
{
    protected $table = "member_waterbill";

    protected $redisListTable = "member_bill_list";

    protected $redisKeyTable = "member_bill_list_keys";

    protected $rules = [
        'admin_id' => 'required',
        'shop_id' => 'required',
        'mid' => 'required',
        'waterbill_id' => 'required',
        'goods_id' => 'required',
        'goods_name' => 'required',
        'rest_num' => 'required|numeric',
    ];

    protected $message = [
        'admin_id.required' => '商户ID获取失败',
        'shop_id.required' => '店铺ID获取失败',
        'mid.required' => '用户ID获取失败',
        'waterbill_id.required' => '水票ID获取失败',
        'goods_id.required' => '商品ID获取失败',
        'goods_name.required' => '商品名称获取失败',
        'rest_num.required' => '剩余总数量获取失败',
        'rest_num.numeric' => '剩余总数量必须是纯数字',
    ];

    public function waterBill()
    {
        return $this->hasOne('App\Models\WaterBill', 'id', 'waterbill_id');
    }


    public function getListByMid($mid)
    {
        $where['mid'] = $mid;
        $fields = 'id,mid,goods_id,goods_name,rest_num,admin_id';
        $rows = $this->_getList($where, $fields, ['rest_num' => 'desc'])->toArray();
        return $rows;
    }


    public function checkData($ids)
    {
        if (!$ids) {
            $this->error = '参数错误';
            return false;
        }

        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['goods_id'] = ['in', $ids];
        $where['shop_id'] = request('shop_id');
        $res = $this->_getList($where);
        if ($res) {
            return false;
        }
        return true;
    }

    public function addorUpdate($billData)
    {
        $memberBillData['admin_id'] = $billData['admin_id'];
        $memberBillData['shop_id'] = $billData['shop_id'];
        $memberBillData['mid'] = $billData['mid'];
        $memberBillData['waterbill_id'] = $billData['waterbill_id'];
        $memberBillData['bucket_category_id'] = $billData['bucket_category_id'];
        $memberBillData['category_id'] = $billData['category_id'];
        $memberBillData['shop_goods_id'] = $billData['shop_goods_id'];
        $memberBillData['goods_id'] = $billData['goods_id'];
        $memberBillData['goods_name'] = $billData['goods_name'];
        $memberBillData['goods_price'] = $billData['goods_price'];
        $memberBillData['rest_num'] = $billData['total_num'];
        $where['mid'] = $billData['mid'];
        $where['waterbill_id'] = $billData['waterbill_id'];
        $where['shop_id'] = $billData['shop_id'];
        $memberBill = $this->_getOne($where);
        if ($memberBill) {
            //当前类型水票信息存在的时候则直接累加
            $memberBillData['rest_num'] = $memberBill['rest_num'] + $billData['total_num'];
            $memberBillData['id'] = $memberBill['id'];
        } else {
            $memberBillData['addtime'] = time();
            if (!$this->_validate($memberBillData)) {
                return false;
            }
        }
        $result = $this->_save($memberBillData);
        if (!$result) {
            $this->error = '保存水票信息失败！';
            return false;
        }
        //更新用户余票数量
        $member = new Member();
        $res = $member->_inc(['id' => $memberBillData['mid']], 'total_waterbill_num', $billData['total_num']);
        if (!$res) {
            $this->error = '更新客户水票信息失败！';
            return false;
        }
        $memberBillData['id'] = $result;
        return $memberBillData;
    }

    public function dealOrder($order_sn)
    {
        //修改数据库订单状态
        $order = $this->memberWaterBill->_getOne(['order_sn' => $order_sn]);
        if (!$order) {
            $this->error = '订单号不存在';
            return false;
        }
        $order = $order->toArray();
        if ($order['pay_status'] == 10) {
            $data['id'] = $order['id'];
            $data['pay_status'] = 20;
            $data['payment_time'] = time();
            $this->_start();
            try {
                $a = $this->_save($data);
//                tolog('a----' . $a);
                //更新水票销量
                $waterBill = new WaterBill();
                $b = $waterBill->_inc(['id' => $order['waterbill_id']], 'sale_num');
//                tolog('b----' . $b);
                //更新店铺营业额
                $shop = new Shop();
                $shopData = [
                    'total_water_bill_num' => "`total_water_bill_num`+1",
                    'sale_money' => "`sale_money`+{$order['order_amount']}",
                ];
                $c = $shop->_updateOrigin(['id' => $order['shop_id']], $shopData);
//                tolog('c----' . $c);
                $this->_commit();
                return true;
            } catch (\Exception $e) {
                $this->_rollBack();
                return false;
            }
        }
    }


    public function importBill($memberInfo, $shopId, $waterbill_id, $rest_num)
    {
        $data['admin_id'] = $memberInfo['admin_id'];
        $data['shop_id'] = $shopId;
        $data['mid'] = $memberInfo['id'];
        $data['waterbill_id'] = $waterbill_id;
        $waterBill = new WaterBill();
        $info = $waterBill->_getOneById($data['waterbill_id']);
        $data['category_id'] = $info['category_id'];
        $data['brand_id'] = $info['brand_id'];
        $data['bucket_category_id'] = $info['bucket_category_id'];
        $data['shop_goods_id'] = $info['shop_goods_id'];
        $data['goods_id'] = $info['goods_id'];
        $data['goods_name'] = $info['goods_name'];
        $data['goods_price'] = $info['goods_price'];
        $data['rest_num'] = $rest_num;
        $data['addtime'] = time();
        if (!$this->_validate($data)) {
            return false;
        }
        return $this->_save($data);
    }

    public function _redisGetBillListByMid($mid)
    {
        if (!$mid) {
            $this->error = '非法操作';
            return false;
        }
        $table = $this->redisListTable;
        $key = $mid;
        if ($this->redis->hexists($table, $key)) {
            $res = $this->redis->hmget($table, $key);
            return unserialize($res[0]);
        }
        $where['mid'] = $mid;
        $fields = 'id,mid,goods_id,goods_name,rest_num,admin_id,shop_goods_id';
        $order['rest_num'] = 'desc';
        $rows = $this->_getList($where, $fields, $order)->toArray();
        $this->redis->hmset($table, [$key => serialize($rows)]);
        return $rows;
    }


    public function _redisGenerateBillListByMid($mid)
    {
        if (!$mid) {
            $this->error = '非法操作';
            return false;
        }
        $table = $this->redisListTable;
        $key = $mid;
        $this->redis->hdel($table, $key);
    }
}
